package com.eksboot.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * TargetProxy
 *
 * @author: chenfenghai
 * @version: 1.0
 * @date: 2021-05-25 14:50
 */
public class TargetProxy implements InvocationHandler {

    private Object target;

    public TargetProxy(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(" 拦截前。。。");
        Object result = method.invoke(target, args);
        System.out.println(" 拦截后。。。");
        return result;
    }

    public static Object wrap(Object target) {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), new TargetProxy(target));
    }

}
